package com.nl.controller.web.runner;

import com.alibaba.fastjson.JSONObject;
import com.nl.controller.web.bean.Send5gResult;
import com.nl.controller.web.global.DealSendResultCLQ;
import com.nl.controller.web.mapper.Sms5gMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.*;

@Component
@Slf4j
public class SendResultDealRunnerImpl implements ApplicationRunner {
	
	@Autowired
	private Sms5gMapper sms5gMapper;
	
	private List<Integer>  update5gSmsReport = new ArrayList<Integer>();
	
	
	@Override
	public void run(ApplicationArguments arg0) throws Exception {
		ThreadFactory SendResultDealThreadFactory = new CustomizableThreadFactory("5g-Send-Result-Thread-pool-");
		ExecutorService executor = new ThreadPoolExecutor(1, 1,
				0L, TimeUnit.MILLISECONDS,
				new LinkedBlockingQueue<Runnable>(),
				SendResultDealThreadFactory);
		for (int i= 0;i<1;i++){
			executor.execute(() ->{
				while (true){
					try{
						Send5gResult dealSendResult = DealSendResultCLQ.sendResultDealQueue.poll();
						if (Objects.isNull(dealSendResult)){
								Thread.sleep(10000);
								log.info("5g sendResultDealQueue has no data to deal,go to sleep 10s");
								continue;
						}
						String taskId = dealSendResult.getTaskId();			            
			            //发送结果 4 成功 5 失败 6 下载成功
			            String status = dealSendResult.getStatus();			            
			            String mobile = dealSendResult.getMobile();
			            String errorCode = dealSendResult.getErrorCode();
			            sms5gMapper.update5gSendResult(dealSendResult);
			            
					}catch (Exception e){
						log.info("5gSendResultDealRunner catch Exception :" +e.getMessage());
						e.printStackTrace();
					}
				}
			});
		}
	}
	
	public  void updateSmsReportDetail(Connection conn, boolean closeConn, Integer seqId){
	 	String sql = "update /*+ parallel(16)*/ sms_report_detail t set t.send_state=-1 where  t.send_time>to_date(?,'yyyymmddhh24miss') and t.seq_id=?";
	 	if (update5gSmsReport.size()<50){
	 		update5gSmsReport.add(seqId);
		} else {
	 		Long time1 = System.currentTimeMillis();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			Date date = new Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, -2);
			date = calendar.getTime();
			String time = sdf.format(date)+"000000";
			Object[][] params = new Object[update5gSmsReport.size()][2];
			for (int i=0;i<update5gSmsReport.size();i++){
				params[i][0] = time;
				params[i][1] = update5gSmsReport.get(i);
			}
			//int[] result = batchQuietly(conn, closeConn, sql,params);
			update5gSmsReport.clear();
			Long time2 = System.currentTimeMillis();
			log.info("update sms report detail once cost:"+(time2-time1));
		}
	}

}
